package com.wgj.android.web;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.webkit.WebSettings;
import android.widget.Toast;

import com.google.gson.Gson;
import com.jiaai.xui.XUI;
import com.jiaai.xui.base.XApplication;
import com.jiaai.xui.jsbridge.BridgeHandler;
import com.jiaai.xui.jsbridge.BridgeWebView;
import com.jiaai.xui.jsbridge.CallBackFunction;
import com.jiaai.xui.view.widget.XToast;
import com.wgj.android.dialog.AddressSelectDialog;
import com.wgj.android.ui.activity.FriendsCleanActivity;
import com.wgj.android.ui.activity.MyBankActivity;
import com.wgj.android.ui.activity.TxActivity;
import com.wgj.android.web.entity.WebConfig;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by Administrator on 2017/10/28.
 */

public class WGJWebFunctionUtils {

    public static void initSetting(BridgeWebView webView){
        webView.getSettings().setTextSize(WebSettings.TextSize.SMALLER);
        // 开启DOM缓存，开启LocalStorage存储（html5的本地存储方式）
        webView.getSettings().setDatabaseEnabled(true);
        webView.getSettings().setJavaScriptEnabled(true);
        // 设置支持缩放
        webView.setHorizontalScrollBarEnabled(false);//水平不显示
        webView.setVerticalScrollBarEnabled(false); //垂直不显示
        webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        // 使用localStorage则必须打开
        webView.getSettings().setDomStorageEnabled(true);
        webView.getSettings().setGeolocationEnabled(true);
        // 设置可以访问文件
        webView.getSettings().setAllowFileAccess(true);
        webView.getSettings().setDatabasePath(webView.getContext().getCacheDir().getAbsolutePath());
    }
    public static  void bindFunction(final BridgeWebView webView, final android.support.v4.app.FragmentManager fragmentManager){
        webView.registerHandler("openWindow", new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {
                    WebConfig config= new Gson().fromJson(data,WebConfig.class);
                    try {
                        Intent intent=new Intent(webView.getContext(),WGJWebActivity.class);
                        intent.putExtra(WGJWebActivity.WEB_CONFIG,config);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        XUI.getContext().startActivity(intent);
                        function.onCallBack("{susscess:1}");
                    }catch (Exception e){
                        function.onCallBack("{susscess:0,error:\""+e.getMessage()+"\"}");
                    }
             }

        });
        webView.registerHandler("exit", new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {

                ((Activity) webView.getContext()).finish();

            }

        });


        webView.registerHandler("cleanFriends", new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {
                try {
                    Intent intent=new Intent(webView.getContext(),FriendsCleanActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    XUI.getContext().startActivity(intent);
                }catch (Exception e){
                    function.onCallBack("{susscess:0,error:\""+e.getMessage()+"\"}");
                }
            }

        });
        webView.registerHandler("openMultiWindow", new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {
                WebConfig config= new Gson().fromJson(data,WebConfig.class);
                try {
                    Intent intent=new Intent(webView.getContext(),WGJWebActivity.class);
                    intent.putExtra(WGJWebActivity.WEB_CONFIG,config);
                    XApplication.getInstance().startActivity(intent);
                    function.onCallBack("{susscess:1}");
                }catch (Exception e){
                    function.onCallBack("{susscess:0,error:\""+e.getMessage()+"\"}");
                }
            }

        });
        webView.registerHandler("myBankCards", new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {
                try {

                    MyBankActivity.openMyBankUI(webView.getContext().getApplicationContext());
                 }catch (Exception e){

                }
            }

        });
        webView.registerHandler("txToBank", new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {
                try {

                    TxActivity.toTx(webView.getContext().getApplicationContext());
                    ((Activity) webView.getContext()).finish();
                }catch (Exception e){
                    Log.e("txToBank",e.getMessage());
                }
            }

        });

        webView.registerHandler("alertMsg", new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {
                try {
                    JSONObject object=new JSONObject(data);
                    switch (object.getString("type")){
                        case "error":
                            XToast.error(object.getString("message"));
                            break;
                        case "warning":
                            XToast.warning(object.getString("message"));
                            break;
                        case "success":
                            XToast.success(object.getString("message"));
                            break;
                    }
                } catch (JSONException e) {
                    Toast.makeText(webView.getContext(),"参数错误",Toast.LENGTH_SHORT).show();
                 }
                Toast.makeText(webView.getContext(),data,Toast.LENGTH_SHORT).show();
            }

        });
        webView.registerHandler("toast", new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {
                Toast.makeText(webView.getContext(),data,Toast.LENGTH_SHORT).show();
            }

        });
        webView.registerHandler("address", new BridgeHandler() {
            @Override
            public void handler(String s, final CallBackFunction callBackFunction) {
                //Intent intent = new Intent((Activity)webView.getContext(), AddressActivity.class);

                try {
                    new AddressSelectDialog().show(fragmentManager,"");
                    EventBus.getDefault().postSticky(callBackFunction);
                }catch (Exception e){
                    Log.e("address",e.getMessage());
                }

            }
        });
    }
}
